package be.kiojeux.dao.connection;

import org.apache.log4j.Logger;
import java.sql.*;

public class RL_StoredProcedures {

	private static final Logger log = Logger.getLogger(RL_StoredProcedures.class);
	/**
	 * @param args
	 */
	public static void main(String[] args) {		
	
		//CHARGEMENT DU DRIVER
	
		try {				
			Class.forName("com.mysql.jdbc.Driver");
		} 
		catch (ClassNotFoundException e) {
			log.error("Chargement driver: Echec");
			e.printStackTrace();
		}
		
		//CONNECTION AU SGBD
		
		//String url = "jdbc:mysql://conceptled.dyndns.org/kiojeux";
		//String url = "jdbc:mysql://localhost:3306/kiojeux";
		//String user = "root";
		//String password = "";
		String url = "jdbc:mysql://instance31669.db.xeround.com:20447/kiojeux";
		String user = "kiojeux";
		String password = "KioJeux789321";
		Connection connection;
		try {
			connection = DriverManager.getConnection(url,user,password);
			log.debug("Connexion SGBD: Ok");
			
			//CREATION DES PROCEDURES
							
			returnAllJeux(connection);
		} 
		catch (SQLException e) {
			log.error("Connexion SGBD: Echec");
			e.printStackTrace();
		}			
	}	
	
	public static void returnAllJeux(Connection connection){
		try {
			Statement statement = connection.createStatement();
			
			statement.executeUpdate("DROP PROCEDURE IF EXISTS ReturnAllJeux");
			statement.executeUpdate(
			"CREATE PROCEDURE ReturnAllJeux()\n"
			+ "BEGIN\n"
			+ "SELECT denomination, description, ageminimum, joueursminimum, joueursmaximum, duree, urljeux FROM jeux;\n"
			+ "END\n");
			statement = null;
			log.debug("Procedure ReturnAllJeux: OK");
		} catch (SQLException e) {
			log.error("Procedure ReturnAllJeux: Echec");
			e.printStackTrace();
		}						
	}
					
}
